package com.uade.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.uade.entities.ArticuloEntity;
import com.uade.model.Modelo;

@Stateless
public class ArticuloDAOImpl extends
		AbstractBaseGenericDAOImpl<ArticuloEntity, Long> implements ArticuloDAO {

	public ArticuloDAOImpl() {
		super();
	}

	@Override
	public List<ArticuloEntity> buscarNombre(String nombre, String categoria) {

		StringBuffer query = new StringBuffer();
		query.append("SELECT a FROM ArticuloEntity a WHERE 1=1");
		if (categoria != null && categoria != "") {
			query.append("AND a.categoria=?");
			query.append("AND a.nombre LIKE :code");
			return em.createQuery(query.toString())
					.setParameter("code", "%" + nombre + "%")
					.setParameter(1, categoria).getResultList();
		} else {
			query.append("AND a.nombre LIKE :code");
			return em.createQuery(query.toString())
					.setParameter("code", "%" + nombre + "%").getResultList();

		}
	}

	@Override
	public List<ArticuloEntity> listarArticulos() {
		return (List<ArticuloEntity>) em.createQuery(
				"SELECT a FROM ArticuloEntity a ").getResultList();
	}

	@Override
	public List<ArticuloEntity> listarArticulosByRanking() {

		return (List<ArticuloEntity>) em.createQuery(
				"SELECT a FROM ArticuloEntity a ORDER BY a.ranking ")
				.getResultList();
	}

	@Override
	public ArticuloEntity findByIdDeposito(Long codigoDeposito) {
		List<ArticuloEntity> articulos = em
				.createQuery(
						"SELECT a FROM ArticuloEntity a WHERE a.codArtDeposito=?")
				.setParameter(1, codigoDeposito).getResultList();

		if (articulos != null && !articulos.isEmpty())
			return articulos.get(0);
		return null;
	}

	@Override
	public List<ArticuloEntity> listarArticulosByOrder(String orden,
			String textoBusqueda, String categoria) {
		String param = "";
		StringBuffer queryStr = new StringBuffer();
		queryStr.append("SELECT a FROM ArticuloEntity a WHERE 1=1");
		Query query = em.createQuery(queryStr.toString());
		String order = "";
		if (orden == null)
			order = "";
		else if (orden.equals(Modelo.precio.toString()))
			order = " ORDER BY a.precio";
		else if (orden.equals(Modelo.categoria.toString()))
			order = " ORDER BY a.categoria";
		else if (orden.equals(Modelo.fecha.toString()))
			order = " ORDER BY a.fecha";
		else if (orden.equals(Modelo.bestSellers.toString()))
			order = " ORDER BY a.ranking";

		if (categoria != null && categoria != "") {
			queryStr.append("AND a.categoria=?");
			queryStr.append("AND a.nombre LIKE :code");

			if (order != "")
				queryStr.append(order);
			query = em.createQuery(queryStr.toString());
			query.setParameter("code", "%" + textoBusqueda + "%");
			query.setParameter(1, categoria);

		} else {
			queryStr.append("AND a.nombre LIKE :code");

			if (order != "") {
				queryStr.append(order);
				query = em.createQuery(queryStr.toString());
				query.setParameter("code", "%" + textoBusqueda + "%");
			}

			
		}
		return (List<ArticuloEntity>) query.getResultList();
	}
}